<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\library\Yunding;
use Carbon\Carbon;
use fast\Random;

/**
 * 首页接口
 */
class Index extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    public function test()
    {
        $start = Carbon::parse('2022-06-11 08:45:00');
        $end = Carbon::parse('2022-06-11 09:00:00');

        echo number_format($end->diffInMinutes($start) / 60, 2);

    }

    public function getPasswd()
    {

        $passwords = [];

        function isSimilar($value1, $value2) {
            $cString = '';
            for ($i = 0; $i < strlen($value1); $i++) {
                if ($value1[$i] == $value2[$i]) {
                    $cString .= '1';
                } else {
                    $cString .= '0';
                }
            }

            if (strstr($cString, '111')) {
                return true;
            } else {
                return false;
            }
        };

        for ($i = 1; $i <= 10; $i++) {
            if (9 == $i) {
                throw new \Exception("连续${i}次没有生成有效密码");
            }
            $passwd = Random::numeric(6);

            $hasSimilar = array_reduce($passwords, function ($a, $b) use ($passwd) {
                if (isSimilar($b, $passwd)) {
                    $a += 1;
                }
                return $a;
            }, 0);

            if ($hasSimilar) {
                continue;
            } else {
                break;
            }
        }

        echo $passwd;
    }

    /**
     * 首页
     *
     */
    public function index()
    {
        $this->success('请求成功');
    }

}
